package com.example.jetcache_demo.service.impl;

import com.alicp.jetcache.Cache;
import com.alicp.jetcache.anno.CreateCache;
import com.alicp.jetcache.embedded.LinkedHashMapCacheBuilder;
import com.example.jetcache_demo.service.TestService;
import jakarta.annotation.PostConstruct;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @Description TODO
 * @Author lihw
 * @Date 2024/7/18 18:00
 */
@Service
public class TestServiceImpl implements TestService {

    @CreateCache(name="userCache", expire = 3600)
    private Cache<String, String> userCache;

    @Override
    public String ttlMsg(String userId) {
        System.out.println("未走缓存");
        return "hello " + userId;
    }

    @Override
    public String localMsg(String userId) {
        userCache.put(userId, userId);
        return "local" + userId;
    }

    @PostConstruct
    public void cacheInit(){
        Cache<Object, Object> localCache = LinkedHashMapCacheBuilder.createLinkedHashMapCacheBuilder()
                .limit(100)
                .expireAfterWrite(3600, TimeUnit.SECONDS)
                .buildCache();

        System.out.println("Cache initialized.");
    }
}
